package com.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContext;

import com.domain.Form1;
import com.domain.LoginForm;
import com.domain.Shop;
import com.domain.User;
import com.service.AdviceManager;

@Controller
@SessionAttributes(value={"loginform","locale"})
// ①让ModelMap的loginform属性拥有session级作用域
public class LoginController {
	@Autowired
    private LocaleResolver localeResolver;
	@Autowired  
	private ApplicationContext appCtx;

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request,LoginForm loginform,ModelMap model,String username) {// 可以将封装好的LoginForm loginform作为入参(自动封装)，也可以将username，password分别作为入参

		// String username = request.getParameter("username");//还可以用老方法，通过request获取参数，,HttpServletRequest request要作为入参
		// username = new String(username.getBytes("ISO-8859-1"),"utf-8"); //解决乱码的终极办法，没办法的办法
		model.addAttribute("loginform",loginform);
		System.out.println(loginform.getUsername());
		return "index/index";
//		return "redirect:"+request.getRequestURI();
	}

	@RequestMapping(value = "JqueryUIAutocomplete")
	public String JqueryUIAutocomplete(ModelMap model,@ModelAttribute("loginform") LoginForm loginform2) {
		LoginForm loginform = (LoginForm)model.get("loginform");
		// 等同于@ModelAttribute("loginform") LoginForm loginform，不过需要ModelMap model作为入参
		if(loginform != null){
			System.out.println("loginform:"+loginform.getUsername()+','+loginform.getPassword());
		}
		return "JqueryUIAutocomplete";
	}

	@RequestMapping(value = "get_person")
	public String get_person() {
		AdviceManager manager = new AdviceManager();
		manager.toBeAdvice();
		// manager.throwingException();
		String result = manager.toBeAround("param1","param2");
		System.out.println("返回值："+result);
		return "get_person";
	}

	@RequestMapping(value = "ichartjs")
	public String ichartjs() {
		return "ichartjs/ichartjs";
	}

	@RequestMapping(value = "echarts")
	public String echarts() {
		return "redirect:/";
		/*redirect:/ 跳转回原页面*/
	}
	@RequestMapping(value = "changeLocale")
	public String changeLocale(String locale,HttpServletRequest request,HttpServletResponse response) {
		
		//locale 格式为 language_country，例如 en_US，zh_CN
        Locale setLocale = new Locale(locale.split("_")[0] , locale.split("_")[1]);
		localeResolver.setLocale(request, response, setLocale);
		
		//设置变量 locale 为当前 Locale
		request.setAttribute("locale", localeResolver.resolveLocale(request));
		
		//按当前 Locale 获取国际化信息
		RequestContext requestContext = new RequestContext(request);		
		System.out.println(requestContext.getMessage("username"));
		System.out.println(requestContext.getMessage("password"));
		
		//按自定义 Locale 获取国际化信息
		System.out.println(appCtx.getMessage("username", null, Locale.CHINA));
		System.out.println(appCtx.getMessage("username", new Object[]{}, Locale.US));
		
		System.out.println(request.getRequestURI());
		
		User user = new User();
		user.setUserName("aaa");
		request.setAttribute("user", user);
		return "locale";
//		return "redirect:/";//跳转回原页面
	}
	
	@RequestMapping(value = "kindeditor", method = RequestMethod.POST)
	public String kindeditor(ModelMap model,String content1) {
		model.addAttribute("content1",content1);
		return "kindeditor";
	}
	
	@RequestMapping(value = "getperson/{name}")
	@ResponseBody
	public List<Shop> getperson(@PathVariable String name) {
		List<Shop> shopList = new ArrayList<Shop>();
		Shop shop = new Shop();
		shop.setName(name+1);
		shop.setStaffName("mkyong1");
		shopList.add(shop);
		Shop shop2 = new Shop();
		shop2.setName(name+2);
		shop2.setStaffName("mktong2");
		shopList.add(shop2);
		return shopList;
	}

	@RequestMapping(value = "jquery_form")
	public String jquery_form(ModelMap model,HttpServletRequest request) {
		model.addAttribute("IP",request.getRemoteAddr());
		model.addAttribute("User",request.getRemoteUser()!=null ? 0 : 1);
		model.addAttribute("requestClass",request.getClass());
		model.addAttribute("requestCookies",request.getCookies());
		model.addAttribute("requestContextPath",request.getContextPath());
		return "jquery_form";
	}

	@RequestMapping(value = "post_jquery_form", method = RequestMethod.POST)
	@ResponseBody
	public Form1 post_jquery_form(Form1 form1) {
		return form1;
	}
}
